<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns:schema_1_0="http://schemas.microsoft.com/ado/2006/04/edm"
    xmlns:schema_1_1="http://schemas.microsoft.com/ado/2007/05/edm"
    xmlns:schema_1_2="http://schemas.microsoft.com/ado/2008/09/edm"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

  <xsl:output method="text"/>

  <!-- Default service URI passed externally -->
  <xsl:param name="QueryProviderVersion"/>
  <xsl:template match="/">&lt;?php
<!--
/*
    Copyright 2011 Persistent Systems Limited
    Licensed under the New BSD License scheme.;
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.opensource.org/licenses/bsd-license.php
*/
    /**
     * This code was generated by the tool 'MySQLConnector.php'.
     * Runtime Version:1.0
     *
     * Changes to this file may cause incorrect behavior and will be lost if
     * the code is regenerated.
     */
-->
/** 
 * Implementation of IDataServiceMetadataProvider.
 * 
 * PHP version 5.3
 * 
 * @category  Service
 * @author    Yash K. Kothari &lt;odataphpproducer_alias@microsoft.com&gt;
 * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
 * @license   New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
 * @version   SVN: 1.0
 * @link      http://odataphpproducer.codeplex.com
 * 
 */
use ODataProducer\Providers\Metadata\ResourceStreamInfo;
use ODataProducer\Providers\Metadata\ResourceAssociationSetEnd;
use ODataProducer\Providers\Metadata\ResourceAssociationSet;
use ODataProducer\Common\NotImplementedException;
use ODataProducer\Providers\Metadata\Type\EdmPrimitiveType;
use ODataProducer\Providers\Metadata\ResourceSet;
use ODataProducer\Providers\Metadata\ResourcePropertyKind;
use ODataProducer\Providers\Metadata\ResourceProperty;
use ODataProducer\Providers\Metadata\ResourceTypeKind;
use ODataProducer\Providers\Metadata\ResourceType;
use ODataProducer\Common\InvalidOperationException;
use ODataProducer\Providers\Metadata\IDataServiceMetadataProvider;
require_once 'ODataProducer/Providers/Metadata/IDataServiceMetadataProvider.php';
use ODataProducer\Providers\Metadata\ServiceBaseMetadata;
//Begin Resource Classes
<xsl:apply-templates select="/edmx:Edmx/edmx:DataServices/schema_1_0:Schema | /edmx:Edmx/edmx:DataServices/schema_1_1:Schema | /edmx:Edmx/edmx:DataServices/schema_1_2:Schema"/>
?&gt;
</xsl:template>
<xsl:template match="/edmx:Edmx/edmx:DataServices/Schema">
    <xsl:for-each select="schema_1_0:EntityType | schema_1_1:EntityType | schema_1_2:EntityType">
        <xsl:for-each select="Property">
          <xsl:apply-templates select="."/>
        </xsl:for-each>
    </xsl:for-each>
    <xsl:for-each select="schema_1_0:Association | schema_1_1:Association | schema_1_2:Association">
          <xsl:apply-templates select="."/>
    </xsl:for-each>
</xsl:template>

<xsl:template match="schema_1_0:Schema |schema_1_1:Schema |schema_1_2:Schema">
 <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" /> 
 <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 
 <xsl:variable name="Namespace" select="@Namespace"/>
 <xsl:for-each select="schema_1_0:EntityType |schema_1_1:EntityType |schema_1_2:EntityType">
 <xsl:variable name="entityTypeName" select="@Name"/>
 <xsl:variable name="entityTypeDBName">
 <xsl:for-each select="../schema_1_0:EntityNameInformation | ../schema_1_1:EntityNameInformation | ../schema_1_2:EntityNameInformation">
   <xsl:for-each select="schema_1_0:EntityName | schema_1_1:EntityName | schema_1_2:EntityName">
     <xsl:if test="@EntityTypeName = $entityTypeName">
       <xsl:value-of select="@dbName"/>
     </xsl:if>
   </xsl:for-each>
 </xsl:for-each>
 </xsl:variable>
/**
 * <xsl:value-of select="@Name"/> entity type.
 * 
 * @category  Service
 * @package   Service_<xsl:value-of select="$Namespace"/>
 * @author    Yash K. Kothari &lt;odataphpproducer_alias@microsoft.com&gt;
 * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
 * @license   New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
 * @version   Release: 1.0
 * @link      http://odataphpproducer.codeplex.com
 */
class <xsl:value-of select="@Name"/>
{<xsl:for-each select="schema_1_0:Property | schema_1_1:Property | schema_1_2:Property">
    //<xsl:value-of select="@Type"/>
    <xsl:variable name="propertyDBName" select="@Name"/>
    <xsl:for-each select="../../schema_1_0:MappingDetails | ../../schema_1_1:MappingDetails | ../../schema_1_2:MappingDetails">
      <xsl:for-each select="schema_1_0:MapEntity | schema_1_1:MapEntity | schema_1_2:MapEntity">
        <xsl:if test="@DBEntityName = $entityTypeDBName">
          <xsl:for-each select="schema_1_0:MapProperty | schema_1_1:MapProperty | schema_1_2:MapProperty">
            <xsl:if test="@DBPropertyName = $propertyDBName">
    public $<xsl:value-of select="@UserPropertyName"/>;
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
<!-- public $<xsl:value-of select="@Name"/>;--></xsl:for-each>
    <xsl:for-each select="schema_1_0:NavigationProperty | schema_1_1:NavigationProperty | schema_1_2:NavigationProperty">
    //Navigation Property <xsl:value-of select="$Namespace"/>.<xsl:value-of select="@ToRole"/>
    public $<xsl:value-of select="@ToRole"/>;
    </xsl:for-each>
}
</xsl:for-each>

/**
 * Create <xsl:value-of select="$Namespace"/> metadata.
 * 
 * @category  Service
 * @package   Service_<xsl:value-of select="$Namespace"/>
 * @author    Yash K. Kothari &lt;odataphpproducer_alias@microsoft.com&gt;
 * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
 * @license   New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
 * @version   Release: 1.0
 * @link      http://odataphpproducer.codeplex.com
 */
class Create<xsl:value-of select="$Namespace"/>Metadata
{
    /**
     * create metadata
     * 
     * @return <xsl:value-of select="$Namespace"/>Metadata
     */
    public static function create()
    {
        $metadata = new ServiceBaseMetadata('<xsl:value-of select="$Namespace"/>Entities', '<xsl:value-of select="$Namespace"/>');
        <xsl:for-each select="schema_1_0:EntityType |schema_1_1:EntityType |schema_1_2:EntityType">
<xsl:variable name="entityTypeName" select="@Name"/>
 <xsl:variable name="entityTypeDBName">
 <xsl:for-each select="../schema_1_0:EntityNameInformation | ../schema_1_1:EntityNameInformation | ../schema_1_2:EntityNameInformation">
   <xsl:for-each select="schema_1_0:EntityName | schema_1_1:EntityName | schema_1_2:EntityName">
     <xsl:if test="@EntityTypeName = $entityTypeName">
       <xsl:value-of select="@dbName"/>
     </xsl:if>
   </xsl:for-each>
 </xsl:for-each>
 </xsl:variable>
        //Register the entity (resource) type '<xsl:value-of select="@Name"/>'
        $<xsl:value-of select="@Name"/>EntityType = $metadata->addEntityType(
            new ReflectionClass('<xsl:value-of select="@Name"/>'), '<xsl:value-of select="@Name"/>', '<xsl:value-of select="$Namespace"/>'
        );
        <xsl:for-each select="schema_1_0:Property | schema_1_1:Property | schema_1_2:Property">
          <xsl:variable name="property" select="@Name" />
          <xsl:variable name="type" select="@Type" />
           <xsl:variable name="keyName">
            <xsl:for-each select="./../schema_1_0:Key | ./../schema_1_1:Key | ./../schema_1_2:Key">
              <xsl:for-each select="schema_1_0:PropertyRef | schema_1_1:PropertyRef | schema_1_2:PropertyRef">
               <xsl:if test="@Name = $property"><xsl:value-of select="@Name"/></xsl:if>
              </xsl:for-each>
            </xsl:for-each>
           </xsl:variable>
         <xsl:choose>
          <xsl:when test="$keyName = @Name">$metadata->addKeyProperty(
            <xsl:variable name="propertyDBName" select="@Name"/>
    <xsl:variable name="propertyUserName">
    <xsl:for-each select="../../schema_1_0:MappingDetails | ../../schema_1_1:MappingDetails | ../../schema_1_2:MappingDetails">
      <xsl:for-each select="schema_1_0:MapEntity | schema_1_1:MapEntity | schema_1_2:MapEntity">
        <xsl:if test="@DBEntityName = $entityTypeDBName">
          <xsl:for-each select="schema_1_0:MapProperty | schema_1_1:MapProperty | schema_1_2:MapProperty">
            <xsl:if test="@DBPropertyName = $propertyDBName">
            <xsl:value-of select="@UserPropertyName"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
    </xsl:variable>$<xsl:value-of select="./../@Name"/>EntityType, '<xsl:value-of select="$propertyUserName"/>', EdmPrimitiveType::<xsl:value-of select="translate(substring-after($type, '.'), $smallcase, $uppercase)"/>
        );
        </xsl:when>
          <xsl:otherwise>$metadata->addPrimitiveProperty(
            <xsl:variable name="propertyDBName" select="@Name"/>
    <xsl:variable name="propertyUserName">
    <xsl:for-each select="../../schema_1_0:MappingDetails | ../../schema_1_1:MappingDetails | ../../schema_1_2:MappingDetails">
      <xsl:for-each select="schema_1_0:MapEntity | schema_1_1:MapEntity | schema_1_2:MapEntity">
        <xsl:if test="@DBEntityName = $entityTypeDBName">
          <xsl:for-each select="schema_1_0:MapProperty | schema_1_1:MapProperty | schema_1_2:MapProperty">
            <xsl:if test="@DBPropertyName = $propertyDBName">
            <xsl:value-of select="@UserPropertyName"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>
    </xsl:variable>$<xsl:value-of select="./../@Name"/>EntityType, '<xsl:value-of select="$propertyUserName"/>', EdmPrimitiveType::<xsl:value-of select="translate(substring-after($type, '.'), $smallcase, $uppercase)"/>
        );
        </xsl:otherwise> 
         </xsl:choose> 
        </xsl:for-each>
        </xsl:for-each>

        <xsl:for-each select="schema_1_0:EntityNameInformation |schema_1_1:EntityNameInformation |schema_1_2:EntityNameInformation">
          <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
        $<xsl:value-of select="@EntitySetName"/>ResourceSet = $metadata->addResourceSet(
            '<xsl:value-of select="@EntitySetName"/>', $<xsl:value-of select="@EntityTypeName"/>EntityType
        );</xsl:for-each>
        </xsl:for-each>

        //Register the assoications (navigations)
        <xsl:for-each select="schema_1_0:Association |schema_1_1:Association |schema_1_2:Association">
           <xsl:variable name="name" select="@Name" />
                <xsl:variable name="entityType">
                  <xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
                     <xsl:value-of select="@Type"/> &amp;&amp;
                  </xsl:for-each>
                </xsl:variable>
                <xsl:variable name="entityType1" select="substring-before($entityType, '&amp;&amp;')" />

                <xsl:variable name="selfAssoc">
                  <xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
                    <xsl:choose>
                      <xsl:when test="normalize-space(@Type) = normalize-space($entityType1)">true</xsl:when>
                      <xsl:otherwise>false</xsl:otherwise></xsl:choose>
                  </xsl:for-each>
                </xsl:variable>
             <xsl:variable name="isSelfAccoc" select="substring-after($selfAssoc, 'true')"/>
             
        <xsl:choose>
          <xsl:when test="$isSelfAccoc = 'true'">
           <xsl:variable name="entitySet"> 
            <xsl:for-each select="./../schema_1_0:EntityNameInformation | ./../schema_1_1:EntityNameInformation | ./../schema_1_2:EntityNameInformation">
              <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
                <xsl:if test="substring-after(normalize-space($entityType1), '.') = normalize-space(@EntityTypeName)">
                   <xsl:value-of select="@EntitySetName" />
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
            </xsl:variable>
        $metadata->addResourceSetReferenceProperty(
            $<xsl:value-of select="substring-after(normalize-space($entityType1), '.')"/>EntityType, '<xsl:value-of select="$entitySet" />', $<xsl:value-of select="$entitySet" />ResourceSet
        );
        </xsl:when><xsl:otherwise>
           <xsl:for-each select="schema_1_0:End | schema_1_1:End | schema_1_2:End">
              <xsl:variable name="role" select="@Role" />
                <xsl:if test="@Multiplicity = '1' or @Multiplicity = '0..1'">
        <xsl:for-each select="./../../schema_1_0:EntityType | ./../../schema_1_1:EntityType | ./../../schema_1_2:EntityType">
        <xsl:for-each select="schema_1_0:NavigationProperty |schema_1_1:NavigationProperty |schema_1_2:NavigationProperty">
        <xsl:if test="$name = @Relationship and $role = @ToRole">
         <xsl:variable name="FromRole" select="@FromRole" />
         <xsl:variable name="toRole">
         <xsl:for-each select="./../../schema_1_0:EntityNameInformation | ./../../schema_1_1:EntityNameInformation | ./../../schema_1_2:EntityNameInformation">
          <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
           <xsl:if test="@EntityTypeName = $role or @EntitySetName = $role">
            <xsl:value-of select="@EntitySetName" />
           </xsl:if>
          </xsl:for-each>
         </xsl:for-each>
         </xsl:variable>
         <xsl:variable name="fromRole">
         <xsl:for-each select="./../../schema_1_0:EntityNameInformation | ./../../schema_1_1:EntityNameInformation | ./../../schema_1_2:EntityNameInformation">
          <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
           <xsl:if test="@EntityTypeName = $FromRole or @EntitySetName = $FromRole">
            <xsl:value-of select="@EntityTypeName" />
           </xsl:if>
          </xsl:for-each>
         </xsl:for-each>
         </xsl:variable>$metadata->addResourceReferenceProperty(
            $<xsl:value-of select="$fromRole"/>EntityType, '<xsl:value-of select="$role"/>', $<xsl:value-of select="$toRole"/>ResourceSet
        );
        </xsl:if>
        </xsl:for-each>
        </xsl:for-each>
                </xsl:if>
                <xsl:if test="@Multiplicity = '*'">
        <xsl:for-each select="./../../schema_1_0:EntityType | ./../../schema_1_1:EntityType | ./../../schema_1_2:EntityType">
        <xsl:for-each select="schema_1_0:NavigationProperty |schema_1_1:NavigationProperty |schema_1_2:NavigationProperty">
        <xsl:if test="$name = @Relationship and $role = @ToRole">
        <xsl:variable name="FromRole" select="@FromRole" />
         <xsl:variable name="toRole">
         <xsl:for-each select="./../../schema_1_0:EntityNameInformation | ./../../schema_1_1:EntityNameInformation | ./../../schema_1_2:EntityNameInformation">
          <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
           <xsl:if test="@EntityTypeName = $role or @EntitySetName = $role">
            <xsl:value-of select="@EntitySetName" />
           </xsl:if>
          </xsl:for-each>
         </xsl:for-each>
         </xsl:variable>
         <xsl:variable name="fromRole">
         <xsl:for-each select="./../../schema_1_0:EntityNameInformation | ./../../schema_1_1:EntityNameInformation | ./../../schema_1_2:EntityNameInformation">
          <xsl:for-each select="schema_1_0:EntityName |schema_1_1:EntityName |schema_1_2:EntityName">
           <xsl:if test="@EntityTypeName = $FromRole or @EntitySetName = $FromRole">
            <xsl:value-of select="@EntityTypeName" />
           </xsl:if>
          </xsl:for-each>
         </xsl:for-each>
         </xsl:variable>$metadata->addResourceSetReferenceProperty(
            $<xsl:value-of select="$fromRole"/>EntityType, '<xsl:value-of select="$role"/>', $<xsl:value-of select="$toRole"/>ResourceSet
        );
        </xsl:if>
        </xsl:for-each>
        </xsl:for-each>
                </xsl:if>
          </xsl:for-each>
          </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each>
        return $metadata;
    }
}</xsl:template>
</xsl:stylesheet>